<template>
  <div>
    <!--<DeviceManageForm  ref="DeviceManageForms"></DeviceManageForm >-->
    <Form ref="scriptObj" :model="scriptObj" :label-width="70">
      <FormItem label="脚本状态" prop="switch" >
        <i-switch size="large" v-model="scriptObj.switch" :true-value="1" :false-value="0">
          <span slot="open">启用</span>
          <span slot="close">停用</span>
        </i-switch>
      </FormItem>
      <FormItem label="脚本代码" prop="codes">
        <Input v-model="scriptObj.codes" type="textarea" :autosize="{minRows: 12,maxRows: 12}"
               placeholder="脚本代码..."></Input>
      </FormItem>
      <FormItem>
        <Row :gutter="10">
          <Col span="4">
            <Button type="primary" @click="doRun" shape="circle" size="small" long :loading="doRunLoading">运行
            </Button>
          </Col>
          <Col span="4">
            <Button type="primary" @click="doSave" shape="circle" size="small" long :loading="doRunLoading2">保存
            </Button>
          </Col>
          <Col span="4">
            <Button type="primary" @click="resetEngines()"    shape="circle" size="small" long :loading="doRunLoading3">保存并重置引擎
            </Button>
          </Col>
        </Row>
      </FormItem>
      <FormItem label="运行日志" prop="logs">
        <Input v-model="scriptObj.logs" type="textarea" :autosize="{minRows: 8,maxRows: 8}"
               placeholder="运行日志..."></Input>
      </FormItem>
    </Form>
  </div>
</template>
<script>
  export default {
    name: 'ScriptPanel',
    mounted: function () {
    },
    data () {
      return {
        loadingReset3: '',
        childObj: {},
        devTypeObj: {},
        doRunLoading: false,
        doRunLoading2: false,
        doRunLoading3: false,
        saveObj: {},
        scriptObj: {
          codes: '',
          logs: '',
          switch: 0
        }
      }
    },
    methods: {
      updateFormdata (data) {
        this.scriptObj.codes = data.script === '' ? '' : data.script
        this.scriptObj.switch = data.scriptSwitch
        this.saveObj = JSON.parse(JSON.stringify(data))
      },
      makeDefault () {
        this.scriptObj = {
          codes: '',
          switch: 0
        }
      },
      handleSubmit1: function () {
        // this.$refs.mychild.handleSubmit()
        this.$refs.DeviceManageForms.saveSubmitReady()
      },
      submitReady: function () {
        let self = this
        self.scriptObj.iotDevType = self.devTypeObj
        console.log(self.scriptObj)
        // let chirldData = {
        //   tab: 'oneTab',
        //   objStr: JSON.stringify(self.scriptObj)
        // }
        // 向父组件传数据
        self.saveSubmitReady()
        // if (self.editOneData.id) {
        //   // 触发父组件函数
        //   self.$emit('updSubmitReady')
        // } else {
        //   self.$emit('saveSubmitReady')
        // }
      },
      saveSubmitReady: function () {
        let self = this
        self.save()
      },
      save: function () {
        console.log('23232')
        let postData = this.childObj
        // 获取报警数据
        // 获取脚本
        postData.iotLimits = this.$refs.limitSetting.limitList
        // 获取参数
        postData.devParams = this.$refs.devParams.devParamsList
        postData.scriptSwitch = this.scriptObj.switch
        console.log('script...' , this.scriptObj.codes)
        postData.script = this.scriptObj.codes === '' ? '' : this.scriptObj.codes
        window.myapi.dev_save(postData).then((res) => {
          if (res.data.status === 200) {
            this.$Message.success('保存成功!')
            this.modalHide()
            this.resetModalData()
            this.pageObj.index = 0
            this.query()
          } else {
            let msg = res.data.msg ? res.data.msg : '保存失败！'
            this.$Message.error(msg)
          }
        }).catch(function (e) {
          let msg = e.response.data.message ? e.response.data.message : '保存失败!'
          this.$Message.error({ duration: 0, closable: true, content: msg })
        })
      },
      query: function (str) {
        if (str) {
          this.queryStr = str
          this.pageObj.index = 0
        } else {
          this.queryStr = ''
        }
        this.list()
      },
      list: function () {
        let self = this
        let getData = {
          'size': self.pageObj.size,
          'page': self.pageObj.index,
          'iotDevType.brand_sn_name_commAddr_iotDevType.useType_iotSpace.name_plike': self.queryStr
        }
        self.spinShow = true
        window.myapi.dev_list(getData).then(function (res) {
          self.tableList = []
          if (res.data.status === 200) {
            self.tableList = res.data.data.content
            self.totalElements = res.data.data.totalElements
            self.pageObj.size = res.data.data.size
            self.pageObj.total = res.data.data.totalElements
          } else {
            let msg = res.data.msg ? res.data.msg : '请求数据失败!'
            self.$Message.error({ content: msg, duration: 5, closable: true })
          }
          self.spinShow = false
        }).catch(function (e) {
          if (e && e.response && e.response.data) {
            let msg = e.response.data.message ? e.response.data.message : '请求数据失败!'
            self.$Message.error({ content: msg, duration: 5, closable: true })
            self.tableList = []
            self.spinShow = false
          }
        })
      },
      doRun () {
        this.doRunLoading = true
        this.saveObj.script = this.scriptObj.codes
        window.myapi.post_json('/iot/dev/run', this.saveObj).then(res => {
          console.log('res', res)
          this.scriptObj.logs = res.data.data
          // todo  this.scriptObj.logs = res.xx
        })
      },
      doSave () {
        this.$emit('sonCallSave')
      },
      resetEngines () {
        this.loadingReset3 = true
        this.$emit('sonCallSave')
        setTimeout(() => {
          window.myapi.do_get('/iot/dev/resetDevInfo').then(resp => {
            setTimeout(() => {
              this.loadingReset3 = false
            }, 2222)
          })
        }, 1000)
        //
        // this.$emit('resetAndSave')
        //   this.$emit('resetAndSave1')
      }
    }
  }
</script>

<style scoped>

</style>
